package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverflowListDao;
import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.entity.vo.GoodsDamageVo;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OverflowListGoodsServiceImpl
 * @Author 波
 * @Date 2024/9/25 10:06
 */
@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {
    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;
    @Autowired
    private OverflowListDao overflowListDao;
    @Autowired
    private UserDao userDao;

    @Override
    @SneakyThrows
    @Transactional
    public void save(OverflowList overflowList, String overflowListGoodsStr) {
        ObjectMapper objectMapper = new ObjectMapper();
        List<GoodsDamageVo> goodsDamageVoList = objectMapper.readValue(overflowListGoodsStr, new TypeReference<List<GoodsDamageVo>>() {
        });
        overflowList.setUserId(1);
        User user = userDao.getUserById(1);
        overflowList.setTrueName(user.getUserName());
        overflowListDao.save(overflowList);
        OverflowList overflowList1 = overflowListDao.getIdByOverflowNumber(overflowList.getOverflowNumber());
        Assert.notNull(goodsDamageVoList, "报损商品不能为空");
        for (GoodsDamageVo goodsDamageVo : goodsDamageVoList) {
            //保存报损的商品信息
            overflowListGoodsDao.save(goodsDamageVo, overflowList1.getOverflowListId());
        }

    }

    @Override
    public Map<String, Object> list(String sTime, String eTime) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowList> overflowListList = overflowListDao.overflowListByTime(sTime, eTime);
        map.put("rows", overflowListList);
        return map;
    }

    @Override
    public Map<String, Object> getGoodsList(Integer overflowListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowList> overflowListGoodsList = overflowListGoodsDao.getGoodsList(overflowListId);
        map.put("rows", overflowListGoodsList);
        return map;
    }
}
